package com.zrar.ak.gkpt.service.impl.record;


import com.sun.org.apache.xpath.internal.FoundIndex;
import com.zrar.ak.gkpt.dao.IBusinessAccountBasicInfoDao;
import com.zrar.ak.gkpt.dao.IBusinessAccountExpandInfoDao;
import com.zrar.ak.gkpt.dao.IBusinessFundManageAdjustFundDetailsDao;
import com.zrar.ak.gkpt.dao.IBusinessFundManageFundDetailsDao;

import com.zrar.ak.gkpt.model.bo.BusinessAccountBasicInfoBO;
import com.zrar.ak.gkpt.model.bo.BusinessAccountExpandInfoBO;
import com.zrar.ak.gkpt.model.bo.BusinessFundManageAdjustFundDetailsBO;
import com.zrar.ak.gkpt.model.bo.BusinessFundManageFundDetailsBO;
import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.record.IBusinessFundManageService;
import com.zrar.ak.gkpt.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BusinessFundManageService
 * @Description 资金管理Service层
 * @Author HuangKeXin
 * @Time 2020/04/4
 */
@Service
public class BusinessFundManageService implements IBusinessFundManageService {

    /*@Override
    public JsonResult foundManageDetailsSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        return null;
    }

    @Override
    public JsonResult foundManageDetailsUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        return null;
    }

    @Override
    public JsonResult foundManageAdjustSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        return null;
    }

    @Override
    public JsonResult foundManageAdjustUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        return null;
    }

    @Override
    public JsonResult foundManageDetailsGetByUnitId(JsonResult jsonResult, String unitId) {
        return null;
    }

    @Override
    public JsonResult deleteFoundManageDetailsByUnitId(JsonResult jsonResult, String unitId, String foundType) {
        return null;
    }

    @Override
    public JsonPageResult getFoundManageDetailsList(JsonPageResult jsonResult, int currentPage, int pageSize, String unitName) {
        return null;
    }*/
    @Autowired
    private IBusinessFundManageFundDetailsDao businessFundManageFundDetailsDao;
    @Autowired
    private IBusinessFundManageAdjustFundDetailsDao businessFundManageAdjustFundDetailsDao;

    @Override
    public JsonResult foundManageDetailsSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessFundManageFundDetailsBO businessFundManageFundDetailsBO = new BusinessFundManageFundDetailsBO();
        try {
            businessFundManageFundDetailsBO =  CommonTools.jsonStrToObject(formDataJson, BusinessFundManageFundDetailsBO.class);
            String unitId = businessFundManageFundDetailsBO.getUnitId();
            //判断对应的下级单位是否有已存在台账基本信息，若有则无法执行insert操作，若无可继续执行
            int accountBasicInfoTotal = businessFundManageFundDetailsDao.queryExistCountByFoundManageDetailsUnitId(unitId);
            if(accountBasicInfoTotal>0){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_EXISTENT_DATA);
            }else{
                String creatorId = userVO.getUserId();
                Date createTime = new Date();
                String updateUserId = userVO.getUserId();
                Date updateTime = new Date();
                businessFundManageFundDetailsBO.setId(CommonTools.getUUID());
                businessFundManageFundDetailsBO.setCreatorId(creatorId);
                businessFundManageFundDetailsBO.setCreateTime(createTime);
                businessFundManageFundDetailsBO.setUpdateUserId(updateUserId);
                businessFundManageFundDetailsBO.setUpdateTime(updateTime);
                businessFundManageFundDetailsBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int insert = businessFundManageFundDetailsDao.insert(businessFundManageFundDetailsBO);
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    @Override
    public JsonResult foundManageDetailsUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessFundManageFundDetailsBO businessFundManageFundDetailsBO = new BusinessFundManageFundDetailsBO();
        String updateUserId = userVO.getUserName();
        Date updateTime = new Date();
        try {
            businessFundManageFundDetailsBO = CommonTools.jsonStrToObject(formDataJson, BusinessFundManageFundDetailsBO.class);
            BusinessFundManageFundDetailsBO businessFundInfo = businessFundManageFundDetailsDao.selectByPrimaryKey(businessFundManageFundDetailsBO.getId());


            if(businessFundInfo==null || BusinessEnum.IS_DELETE_Y.getValue().equals(businessFundInfo.getIsDelete())){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
            }else if(!businessFundInfo.getUnitId().equals(businessFundManageFundDetailsBO.getUnitId())){
                //如果同一主键的数据库表记录的单位ID和传入数据的单位ID值不一样，则返回业务异常错误
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            }else {
                businessFundManageFundDetailsBO.setCreatorId(businessFundInfo.getCreatorId());
                businessFundManageFundDetailsBO.setCreateTime(businessFundInfo.getCreateTime());
                businessFundManageFundDetailsBO.setUpdateUserId(updateUserId);
                businessFundManageFundDetailsBO.setUpdateTime(updateTime);
                businessFundManageFundDetailsBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int i = businessFundManageFundDetailsDao.updateByPrimaryKey(businessFundManageFundDetailsBO);
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    @Override
    public JsonResult foundManageAdjustSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessFundManageAdjustFundDetailsBO businessFundManageAdjustFundDetailsBO = new BusinessFundManageAdjustFundDetailsBO();
        try {
            businessFundManageAdjustFundDetailsBO =  CommonTools.jsonStrToObject(formDataJson, BusinessFundManageAdjustFundDetailsBO.class);
            String unitId = businessFundManageAdjustFundDetailsBO.getUnitId();
            //只有当该下级单位存在台账基本信息且不存在台账扩展信息时，才能执行台账扩展信息的新增操作
            int accountBasicInfoTotal = businessFundManageFundDetailsDao.queryExistCountByFoundManageDetailsUnitId(unitId);
            int accountExpandInfoTotal = businessFundManageAdjustFundDetailsDao.queryExistCountByFoundManageDetailsAdjustUnitId(unitId);
            if(accountBasicInfoTotal>0 && accountExpandInfoTotal==0){
                String creatorId = userVO.getUserId();
                Date createTime = new Date();
                String updateUserId = userVO.getUserId();
                Date updateTime = new Date();
                businessFundManageAdjustFundDetailsBO.setId(CommonTools.getUUID());
                businessFundManageAdjustFundDetailsBO.setCreatorId(creatorId);
                businessFundManageAdjustFundDetailsBO.setCreateTime(createTime);
                businessFundManageAdjustFundDetailsBO.setUpdateUserId(updateUserId);
                businessFundManageAdjustFundDetailsBO.setUpdateTime(updateTime);
                businessFundManageAdjustFundDetailsBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int insert = businessFundManageAdjustFundDetailsDao.insert(businessFundManageAdjustFundDetailsBO);
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            }else if(accountExpandInfoTotal>0){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_EXISTENT_DATA);
            }else {
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    @Override
    public JsonResult foundManageAdjustUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessFundManageAdjustFundDetailsBO businessFundManageAdjustFundDetailsBO = new BusinessFundManageAdjustFundDetailsBO();
        String updateUserId = userVO.getUserName();
        Date updateTime = new Date();
        try {
            businessFundManageAdjustFundDetailsBO = CommonTools.jsonStrToObject(formDataJson, BusinessFundManageAdjustFundDetailsBO.class);
            BusinessFundManageAdjustFundDetailsBO AdjustFundInfo = businessFundManageAdjustFundDetailsDao.selectByPrimaryKey(businessFundManageAdjustFundDetailsBO.getId());
            if(AdjustFundInfo == null || BusinessEnum.IS_DELETE_Y.getValue().equals(AdjustFundInfo.getIsDelete())){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
            }else if(!AdjustFundInfo.getUnitId().equals(businessFundManageAdjustFundDetailsBO.getUnitId())){
                //如果同一主键的数据库表记录的单位编码和传入数据的单位编码值不一样，则返回业务异常错误
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            }else{
                businessFundManageAdjustFundDetailsBO.setCreatorId(AdjustFundInfo.getCreatorId());
                businessFundManageAdjustFundDetailsBO.setCreateTime(AdjustFundInfo.getCreateTime());
                businessFundManageAdjustFundDetailsBO.setUpdateUserId(updateUserId);
                businessFundManageAdjustFundDetailsBO.setUpdateTime(updateTime);
                businessFundManageAdjustFundDetailsBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int i = businessFundManageAdjustFundDetailsDao.updateByPrimaryKey(businessFundManageAdjustFundDetailsBO);
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }

    @Override
    public JsonResult foundManageDetailsGetByUnitId(JsonResult jsonResult, String unitId) {
        Map map = new HashMap();
        BusinessFundManageFundDetailsBO businessFundManageFundDetailsBO = new BusinessFundManageFundDetailsBO();
        businessFundManageFundDetailsBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        businessFundManageFundDetailsBO.setUnitId(unitId);
        BusinessFundManageAdjustFundDetailsBO businessFundManageAdjustFundDetailsBO = new BusinessFundManageAdjustFundDetailsBO();
        businessFundManageAdjustFundDetailsBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        businessFundManageAdjustFundDetailsBO.setUnitId(unitId);
        businessFundManageFundDetailsBO = businessFundManageFundDetailsDao.selectOne(businessFundManageFundDetailsBO);
        businessFundManageAdjustFundDetailsBO = businessFundManageAdjustFundDetailsDao.selectOne(businessFundManageAdjustFundDetailsBO);
        map.put("businessFundManageFundDetailsBO",businessFundManageFundDetailsBO);
        map.put("businessFundManageAdjustFundDetailsBO",businessFundManageAdjustFundDetailsBO);
        jsonResult.setData(map);
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    @Override
    public JsonResult deleteFoundManageDetailsByUnitId(JsonResult jsonResult, String unitId, String foundType) {
        /**
         * foundType---资金管理类型(1:资金基本信息和资金调整信息执行删除操作 2:资金调整信息执行删除操作)
         */
        if("1".equals(foundType)){
            businessFundManageFundDetailsDao.deleteFundManageByUnitId(unitId);
            businessFundManageAdjustFundDetailsDao.deleteFundManageAdjustByUnitId(unitId);
        }else if("2".equals(foundType)){
            businessFundManageAdjustFundDetailsDao.deleteFundManageAdjustByUnitId(unitId);
        }
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    @Override
    public JsonPageResult getFoundManageDetailsList(JsonPageResult jsonResult, int currentPage, int pageSize, String unitName) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        if(StringUtils.isEmpty(unitName)){
            unitName = null;
        }
        Map map = new HashMap();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("unitName",unitName);
        //返回前端的具体的数据
        List list = null;
        //符合展示条件的数据总数
        Integer total = null;
        total = businessFundManageFundDetailsDao.queryFoundManageShowNum(map);
        list = businessFundManageFundDetailsDao.pagingQueryFoundManageList(map);
        //total = accountInformationDao.queryAccountInformationShowNum(map);
        //list = accountInformationDao.pagingQueryAccountInformationList(map);
        if(total==null){
            total=0;
        }
        //计算一共多少页
        Integer pageNum = (total%pageSize==0)?(total/pageSize):(total/pageSize+1);

        jsonResult.setData(list);
        jsonResult.setCurrentPage(currentPage);
        jsonResult.setPageSize(pageSize);
        jsonResult.setTotalCount(total);
        jsonResult.setTotalPage(pageNum);
        if(list == null || list.size()==0){
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
        }else{
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        }
        return jsonResult;
    }

}